package com.city.java.feedbacks.controller;

import com.city.java.base.utils.PageVO;
import com.city.java.feedbacks.pojo.entity.Feedbacks;
import com.city.java.feedbacks.service.FeedbackService;
import com.city.java.base.response.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 意见反馈控制器
 * RestController注解: 组合注解，相当于@Controller + @ResponseBody
 * Api注解: Knife4j文档中模块的显示名称
 * RequestMapping注解: 设置统一的请求路径前缀
 */
@Slf4j
@Api(tags = "02.意见反馈模块")
@RestController
@RequestMapping("/api/feedbacks")
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    /**
     * 添加反馈
     * PostMapping注解: 处理POST请求
     * ApiOperation注解: Knife4j文档中接口的显示名称
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加反馈", notes = "添加新反馈信息")
    public JsonResult addFeedback(
            @ApiParam(value = "反馈信息", required = true)
            @RequestBody Feedbacks feedback) {
        log.info("接收到添加反馈请求: {}", feedback);
        return feedbackService.addFeedback(feedback);
    }

    /**
     * 根据反馈ID查询反馈
     * GetMapping注解: 处理GET请求
     * PathVariable注解: 获取路径中的参数
     */
    @GetMapping("/select/id/{feedbackId}")
    @ApiOperation(value = "根据ID查询反馈", notes = "根据反馈ID查询反馈详细信息")
    public JsonResult getFeedbackById(
            @ApiParam(value = "反馈ID", required = true, example = "1")
            @PathVariable Long feedbackId) {
        log.info("接收到根据ID查询反馈请求: ID={}", feedbackId);
        return feedbackService.getFeedbackById(feedbackId);
    }

    /**
     * 根据用户ID查询反馈列表
     * RequestParam注解: 获取请求参数
     */
    @GetMapping("/select/id/{userId}")
    @ApiOperation(value = "根据用户ID查询反馈", notes = "根据用户ID查询反馈列表")
    public JsonResult getFeedbacksByUserId(
            @ApiParam(value = "用户ID", required = true, example = "1")
            @PathVariable Long userId) {
        log.info("接收到根据用户ID查询反馈请求: userId={}", userId);
        return feedbackService.getFeedbacksByUserId(userId);
    }

    /**
     * 根据状态查询反馈列表
     */
    @GetMapping("/select/{status}")
    @ApiOperation(value = "根据状态查询反馈", notes = "根据状态查询反馈列表")
    public JsonResult getFeedbacksByStatus(
            @ApiParam(value = "状态", required = true, example = "PENDING")
            @PathVariable Feedbacks.FeedbackStatus status) {
        log.info("接收到根据状态查询反馈请求: status={}", status);
        return feedbackService.getFeedbacksByStatus(status);
    }

    /**
     * 查询所有反馈列表
     */
    @GetMapping("/quarry")
    @ApiOperation(value = "查询所有反馈", notes = "查询所有反馈列表")
    public JsonResult getAllFeedbacks() {
        log.info("接收到查询所有反馈请求");
        return feedbackService.getAllFeedbacks();
    }

    /**
     * 根据反馈ID删除反馈
     * DeleteMapping注解: 处理DELETE请求
     */
    @DeleteMapping("/delete/id/{feedbackId}")
    @ApiOperation(value = "根据ID删除反馈", notes = "根据反馈ID删除反馈")
    public JsonResult deleteFeedbackById(
            @ApiParam(value = "反馈ID", required = true, example = "1")
            @PathVariable Long feedbackId) {
        log.info("接收到根据ID删除反馈请求: ID={}", feedbackId);
        return feedbackService.deleteFeedbackById(feedbackId);
    }

    /**
     * 更新反馈信息
     * PutMapping注解: 处理PUT请求
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新反馈信息", notes = "更新反馈详细信息")
    public JsonResult updateFeedback(
            @ApiParam(value = "反馈信息", required = true)
            @RequestBody Feedbacks feedback) {
        log.info("接收到更新反馈信息请求: {}", feedback);
        return feedbackService.updateFeedback(feedback);
    }

    @GetMapping("/select")
    public JsonResult select(@RequestParam(required = false) String userName,
                             @RequestParam(required = false) Feedbacks.FeedbackStatus status,
                             @RequestParam(required = false) LocalDateTime minDate,
                             @RequestParam(required = false) LocalDateTime maxDate,
                             @RequestParam Integer pageNum,
                             @RequestParam Integer pageSize){

        log.debug("接收到的前端数据:{},{},{},{},{},{}",userName,status,minDate,maxDate,pageNum,pageSize);
        PageVO<Feedbacks> list = feedbackService.select(userName,status,minDate,maxDate,pageNum,pageSize);
        return JsonResult.ok(list);
    }
}
